﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Threading;
using DAO;

namespace GUI
{
    public partial class usThemPin : UserControl
    {
        public usThemPin()
        {
            InitializeComponent();
        }
        private List<int> _dsThayDoi = new List<int>();//true: Xóa | false: Update
        private bool _isInsert = true;
        private int _rowSelect = 0;
        private void btnthem_Click(object sender, EventArgs e)
        {
            if (_isInsert == true)
            {
                ThemPin();
                ResetAll();
                return;
            }
            if (gvPin.Rows.Count > 1)
            {
                tsSave.Enabled = true;
                ThemThayDoi();
                gvPin.Rows.Remove(gvPin.CurrentRow);
            }
        }
        private void ThemPin()
        {
            PinDTO _item = new PinDTO();
            PinBUS _itemBUS = new PinBUS();
            if (txtMaTB.Text == "")
                _item.MATB = "NULL";
            else
            {
                _item.MATB = txtMaTB.Text;
            }

            _item.TENTB = txtCS.Text+"W";
            _item.NGAYNHAP = DateTime.Now;
            _item.VMIN = float.Parse(txtVMin.Text);
            _item.VMAX = float.Parse(txtVMax.Text);
            _item.AMIN = float.Parse(txtAMin.Text);
            _item.AMAX = float.Parse(txtAMax.Text);
            _item.CSKHADUNG = int.Parse(txtCS.Text);
            _item.KICHTHUOC = txtKT.Text;
            _item.DONGIA = 0;
            _item.MALOAIPIN = int.Parse(cbbLoaiPin.SelectedValue.ToString());

            if (_itemBUS.ThemPin(_item) == 1)
                Load_Pin();
        }

        private void SuaPin(int j)
        {
            PinDTO _item = new PinDTO();
            PinDAO _itemBUS = new PinDAO();
            _item.STT_PIN = int.Parse(gvPin.Rows[j].Cells["clSTT_Pin"].Value.ToString());
            if (gvPin.Rows[j].Cells["clMaTB"].Value.ToString() == "")
                _item.MATB = "NULL";
            else
                _item.MATB = gvPin.Rows[j].Cells["clMaTB"].Value.ToString();
            _item.TENTB = gvPin.Rows[j].Cells["clTenPin"].Value.ToString();
            _item.VMIN = float.Parse(gvPin.Rows[j].Cells["clVMin"].Value.ToString());
            _item.VMAX = float.Parse(gvPin.Rows[j].Cells["clVMax"].Value.ToString());
            _item.AMIN = float.Parse(gvPin.Rows[j].Cells["clAMin"].Value.ToString());
            _item.AMAX = float.Parse(gvPin.Rows[j].Cells["clAMax"].Value.ToString());
            _item.CSKHADUNG = int.Parse(gvPin.Rows[j].Cells["clCSKhaDung"].Value.ToString());
            _item.KICHTHUOC = gvPin.Rows[j].Cells["clKichThuoc"].Value.ToString();
            _item.DONGIA = 0;
            _item.MALOAIPIN = int.Parse(gvPin.Rows[j].Cells["clMaLoaiPin"].Value.ToString());
            _itemBUS.SuaPin(_item);
        }

        private void CapNhat()
        {
            PinBUS _pinBUS = new PinBUS();
            for (int i = 0; i < _dsThayDoi.Count; i++)
            {
                bool flag = true;
                int n = gvPin.Rows.Count - 1;
                for (int j = 0; j < n; j++)
                {
                    if (_dsThayDoi[i] == int.Parse(gvPin.Rows[j].Cells["clSTT_Pin"].Value.ToString()))
                    {
                        SuaPin(j);
                        flag = false;
                        continue;
                    }
                }

                if (flag == true)
                    _pinBUS.XoaPin(_dsThayDoi[i]);
            }
            Load_Pin();
        }

        private void tsThem_Click(object sender, EventArgs e)
        {
            _isInsert = true;
            ResetAll();
            btnthem.Text = "Thêm";
            grbPin.Visible = true;

            tsThem.Enabled = false;
            tsUpdate.Enabled = true;
            tsSave.Enabled = false;
            _rowSelect = 0;
        }

        private void a_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Lấy ký tự phân cách thập phân trên nền này
            // ("." đối với US-English).
            int _flag=-1;
            if (((TextBox)sender).Name == "txtCS")
                _flag = -2;
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && ((TextBox)sender).Text.IndexOf(decimalString) == _flag)
            {
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Load_Pin()
        {
            PinBUS _pinBUS = new PinBUS();
            gvPin.DataSource = _pinBUS.LoadPin();
        }

        private void textbox_Leave(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = "0";
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            grbPin.Visible = false; 
            spr_1.Visible = false;
            spr_2.Visible = false;

            tsUpdate.Visible = false;

            tsSave.Visible = false;
            tsSave.Visible = false;

            _dsThayDoi.Clear();
        }

        private void ResetAll()
        {
            txtAMax.Text = "0";
            txtAMin.Text = "0";
            txtVMax.Text = "0";
            txtVMin.Text = "0";
            txtMaTB.Text = "";
            txtCS.Text = "0";
            cbbLoaiPin.SelectedIndex = 0;
            txtKT.Text = "1x1x1";
        }

        private void tsSave_Click(object sender, EventArgs e)
        {
            if (_dsThayDoi.Count < 1)
                return;
            DialogResult dialogResult = MessageBox.Show("Bạn thật sự muốn lưu thay đổi", "Cảnh báo XÓA!", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                //do something
                CapNhat();
            }
            _dsThayDoi.Clear();
            tsSave.Enabled = false;
            Load_Pin();
            Load_to_TextBox();
            ThemThayDoi();
        }

        private void DanhSachThayDoi_Click(object sender, EventArgs e)
        {
            gvPin.Rows[_rowSelect].Selected = true ;
            btnthem.Text = "Xóa";
            _isInsert = false;

            tsUpdate.Enabled = false;
            tsThem.Enabled = true;
            tsSave.Enabled = false;

            gvPin_CellEnter(null, null);
        }

        private void Load_to_TextBox()
        {
            if (gvPin.CurrentRow.Cells["clMaTB"].Value.ToString() == "")
                txtMaTB.Text = "NULL";
            else
                txtMaTB.Text = gvPin.CurrentRow.Cells["clMaTB"].Value.ToString();
            txtMaTB.Text=txtMaTB.Text.Trim(' ');
            for (int i = 0; i < cbbLoaiPin.Items.Count; i++)
            {
                object _temp = cbbLoaiPin.Items[i];
                if (((DataRowView)_temp).Row[0].ToString() == gvPin.CurrentRow.Cells["clMaLoaiPin"].Value.ToString())
                {
                    cbbLoaiPin.SelectedIndex = i;
                    break;
                }
            }
            dateTimePicker1.Text = gvPin.CurrentRow.Cells["clNgayNhap"].Value.ToString();
            txtVMin.Text = gvPin.CurrentRow.Cells["clVMin"].Value.ToString();
            txtVMax.Text = gvPin.CurrentRow.Cells["clVMax"].Value.ToString();
            txtAMin.Text = gvPin.CurrentRow.Cells["clAMin"].Value.ToString();
            txtAMax.Text = gvPin.CurrentRow.Cells["clAMax"].Value.ToString();
            txtCS.Text = gvPin.CurrentRow.Cells["clCSKhaDung"].Value.ToString();
            txtKT.Text = gvPin.CurrentRow.Cells["clKichThuoc"].Value.ToString();
            txtGia.Text = gvPin.CurrentRow.Cells["clDonGia"].Value.ToString();
        }

        private void TinhChinh()
        {
            _isInsert = true;
            if (grbPin.Visible == false)
            {
                grbPin.Visible = true;
                spr_1.Visible = true;
                spr_2.Visible = true;

                tsUpdate.Visible = true;
                tsUpdate.Enabled = true;

                tsThem.Visible = true;
                tsThem.Enabled = false;

                tsSave.Visible = true;
                tsSave.Enabled = false;
                tsThem_Click(null, null);
            }
            else
            {
                grbPin.Visible = false;
                spr_1.Visible = false;
                spr_2.Visible = false;

                tsUpdate.Visible = false;

                tsSave.Visible = false;
                tsSave.Visible = false;

                tsThem.Visible = false;
                _dsThayDoi.Clear();
                Load_Pin();
            }
            //object o = Properties.Resources.ResourceManager.GetObject(channel);
            //if (o is Image)
            //{
            //    tsDongMo.Image = o as Image;
            //}
        }

        private void usThemPin_Load(object sender, EventArgs e)
        {
            SetupDataGridView(gvPin);
            LoaiPinBUS _lp = new LoaiPinBUS();
            cbbLoaiPin.DataSource = _lp.LoadLoaiPin();
            cbbLoaiPin.DisplayMember = "TenLoai";
            cbbLoaiPin.ValueMember = "MaLPin";
            Load_Pin();
        }

        private void gvPin_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (_isInsert == false)
            {
                _rowL = true;
                if (gvPin.CurrentRow.Index == gvPin.Rows.Count - 1)
                    return;
                Load_to_TextBox();
            }
        }

        private void txtVMax_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clVMax"].Value=txtVMax.Text;
            }
        }

        private void txtVMin_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clVMin"].Value=txtVMin.Text;
            }
        }

        private void txtAMin_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clAMin"].Value=txtAMin.Text;
            }
        }

        private void txtAMax_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clAMax"].Value=txtAMax.Text;
            }
        }

        private void txtCS_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clCSKhaDung"].Value = txtCS.Text;
                gvPin.CurrentRow.Cells["clTenPin"].Value = txtCS.Text + "W";
                if (_rowL == false)
                {
                    tsSave.Enabled = true;
                    DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
                    fooCellStyle.BackColor = System.Drawing.Color.Red;
                    gvPin.CurrentRow.Cells["clCSKhaDung"].Style.ApplyStyle(fooCellStyle);
                }
            }
        }

        private void txtKT_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clKichThuoc"].Value=txtKT.Text;

                if (_rowL == false)
                {
                    tsSave.Enabled = true;
                    DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
                    fooCellStyle.BackColor = System.Drawing.Color.Red;
                    gvPin.CurrentRow.Cells["clKichThuoc"].Style.ApplyStyle(fooCellStyle);
                }
            }
        }

        private void txtGia_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                textbox_Leave(sender, e);
                gvPin.CurrentRow.Cells["clDonGia"].Value=txtGia.Text;
            }
        }

        private void txtMaTB_TextChanged(object sender, EventArgs e)
        {
            if (_isInsert == false)
            {
                if (txtMaTB.Text == "")
                    gvPin.CurrentRow.Cells["clMaTB"].Value = "NULL";
                else
                    gvPin.CurrentRow.Cells["clMaTB"].Value = txtMaTB.Text;

                if (_rowL == false)
                {
                    tsSave.Enabled = true;
                    DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
                    fooCellStyle.BackColor = System.Drawing.Color.Red;
                    gvPin.CurrentRow.Cells["clMaTB"].Style.ApplyStyle(fooCellStyle);
                }
            }
        }

        private void gvPin_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            ThemThayDoi();
        }
        private void ThemThayDoi()
        {
            if (_isInsert == false)
            {
                for (int i = 0; i < _dsThayDoi.Count; i++)
                {
                    if (_dsThayDoi[i] == int.Parse(gvPin.CurrentRow.Cells["clSTT_Pin"].Value.ToString()))
                        return;
                }
                _dsThayDoi.Add(int.Parse(gvPin.CurrentRow.Cells["clSTT_Pin"].Value.ToString()));    
            }
        }

        private bool _rowL = false;
        private void gvPin_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            _rowL = false;
        }

        private void cbbLoaiPin_TextChanged(object sender, EventArgs e)
        {

            if (_isInsert == false)
            {
                gvPin.CurrentRow.Cells["clMaLoaiPin"].Value = cbbLoaiPin.SelectedValue;
                gvPin.CurrentRow.Cells["clTenLoai"].Value = cbbLoaiPin.Text;

                if (_rowL == false)
                {
                    tsSave.Enabled = true;
                    DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
                    fooCellStyle.BackColor = System.Drawing.Color.Red;
                    gvPin.CurrentRow.Cells["clTenLoai"].Style.ApplyStyle(fooCellStyle);
                }
            }
        }

        private void btnThemLoai_Click(object sender, EventArgs e)
        {
            frmThemLoaiPin _frmLoai= new frmThemLoaiPin();
            _frmLoai.ShowDialog();

            LoaiPinBUS _lp = new LoaiPinBUS();
            cbbLoaiPin.DataSource = _lp.LoadLoaiPin();
            cbbLoaiPin.DisplayMember = "TenLoai";
            cbbLoaiPin.ValueMember = "MaLPin";
            Load_Pin();
        }

        private void gvPin_DoubleClick(object sender, EventArgs e)
        {
            _rowSelect= gvPin.CurrentRow.Index;
            grbPin.Visible = true;

            spr_1.Visible = true;
            spr_2.Visible = true;

            tsUpdate.Visible = true;
            tsUpdate.Enabled = true;

            tsThem.Visible = true;
            tsThem.Enabled = false;

            tsSave.Visible = true;
            tsSave.Enabled = false;

            DanhSachThayDoi_Click(null, null);
        }

        private void tsQuanLyLoai_Click(object sender, EventArgs e)
        {
            frmThemLoaiPin _frmLoai = new frmThemLoaiPin();
            _frmLoai.ShowDialog();

            LoaiPinBUS _lp = new LoaiPinBUS();
            cbbLoaiPin.DataSource = _lp.LoadLoaiPin();
            cbbLoaiPin.DisplayMember = "TenLoai";
            cbbLoaiPin.ValueMember = "MaLPin";
            Load_Pin();
        }
        private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);


            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }
    }
}
